<%@include file="/WEB-INF/views/includes/taglibs.jsp"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@include file="/WEB-INF/views/includes/adminHeader.jsp"%>

<script type="text/javascript">		
$(document).ready(
		function() {
			$('#disableDialog').dialog({
				autoOpen: false,
				width: 400,
				draggable : true,
				resizable : false,
				dialogClass : 'disableDialog',
				buttons: {
					"Add new Rule": function() {
						$('#operation').val('addARule');
						$('#addBizgroupform').submit();
					},
					"Cancel": function() {
						$(this).dialog("close");
					}
				}
			});
			$('#editFilterRuleDialog').dialog({
				autoOpen: false,
				width: 400,
				draggable : true,
				resizable : false,
				dialogClass : 'disableDialog',
				buttons: {
					"Save": function() {
						$('#editBizgroupform').submit();
					},
					"Cancel": function() {
						$(this).dialog("close");
					}
				}
			});
		});
</script>
<div id="disableDialog" title="Add new Filter Rule">
	<h3>Add new Filter Rule</h3>
	<form:form method="post" action="editFilterRule" id="addBizgroupform">
		<form:input path="bizGroupId" type="hidden"/><form:input path="functionId" type="hidden"/>
		<form:input path="operation" type="hidden"/>
		<div style="font-size:12px;margin-bottom:10px"><span class="label" style="width:150px">Field Name</span><span><form:select path="newRuleFieldName" autocomplete="off">
						 <form:options items="${functionSchemaList}"  itemValue="fieldName" itemLabel="fieldName" />
					</form:select></span></div>
		<div style="font-size:12px;margin-bottom:10px"><span class="label" style="width:150px">Comparator</span><span><form:select path="newRuleComparator" autocomplete="off">
						 <form:options items="${comparatorList}"/>
					</form:select></span></div>
		<div style="font-size:12px;margin-bottom:10px"><span class="label" style="width:150px">value</span><span><form:input path="newRuleValue"/></span></div>
	</form:form>
	<br/>
</div>
<div id="editFilterRuleDialog" title="Edit Filter Rule">
	<h3>Edit Filter Rule</h3>
	<form:form method="post" action="editFilterRule" id="editBizgroupform">
		<form:input path="bizGroupId" type="hidden"/><form:input path="functionId" type="hidden"/>
		<form:input path="operation2" type="hidden"/><form:input path="editRuleId" type="hidden"/>
		<div style="font-size:12px;margin-bottom:10px"><span class="label" style="width:150px">Field Name</span>
		<span><form:select path="updateRuleFieldName" autocomplete="off">
						 <form:options items="${functionSchemaList}"  itemValue="fieldName" itemLabel="fieldName" />
					</form:select></span></div>
		<div style="font-size:12px;margin-bottom:10px"><span class="label" style="width:150px">Comparator</span>
		<span><form:select path="updateRuleComparator" autocomplete="off">
						 <form:options items="${comparatorList}"/>
					</form:select></span></div>
		<div style="font-size:12px;margin-bottom:10px"><span class="label" style="width:150px">value</span>
		<span><form:input path="updateRuleValue"/></span></div>
	</form:form>
	<br/>
</div>
<div class="homePage ui-tabs-panel ui-widget-content ui-corner-bottom">
<div class="introduction">
<p>
<b>Introduction:</b>&nbsp;To improve operational efficiency, only relevant business data is pushed to a specific user<br/>
i.e. a salesman of US branch is only pushed with US sales orders; a technician in China does not care about a maintenance task for US based equipment.
</p>
<p>Your business tasks are always pushed and only pushed to the relevant employee. The distribution rules are defined here, per business group, per each function. </p>
</div>
	<div style="margin-left:10px">
		<%@include file="/WEB-INF/views/includes/messages.jsp"%>
		<h2>Edit Business Group Filter Rule</h2>
		<form:form method="post" action="editFilterRule" id="editFilterRuleform">
		<form:input path="operation1" type="hidden"/>
		<form:input path="operationRuleId" type="hidden"/>
			<c:if test="${command.bizGroupId==0}">
				Please select a Business Group. <br/>
				<form:select path="bizGroupId" autocomplete="off">
					 <form:options items="${bizGroupList}"  itemValue="id" itemLabel="name" />
				</form:select><br/><br/>
				<button type="button"
					class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only"
					onclick="save()">
					<span class="ui-button-text">Continue</span>
				</button>
				<script type="text/javascript">
					function save() {
						$('#editFilterRuleform').submit();
					}
				</script>
			</c:if>
			
			<c:if test="${command.bizGroupId>0}">
				Selected Business Group: <b>${command.bizGroupName}</b> &nbsp;&nbsp;&nbsp;&nbsp;<a href="<c:url value='/web/editFilterRule'/>">change Business Group</a>
				<form:input path="bizGroupId" type="hidden"/><br/><br/>
				
				<c:if test="${command.functionId == null || empty command.functionId}">
					Please select a Function. <br/>
					<form:select path="functionId" autocomplete="off">
						 <form:options items="${functionList}"  itemValue="functionID" itemLabel="label" />
					</form:select><br/><br/>
					<button type="button"
						class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only"
						onclick="save()">
						<span class="ui-button-text">Continue</span>
					</button>
					<script type="text/javascript">
						function save() {
							$('#editFilterRuleform').submit();
						}
					</script>
				</c:if>
				<c:if test="${command.functionId != null && not empty command.functionId}">
					Selected Function: <b>${command.functionName}(${command.functionId})</b> &nbsp;&nbsp;&nbsp;&nbsp;<a href="<c:url value='/web/editFilterRule?bizGroupId='/>${command.bizGroupId}">change Function</a>
					<form:input path="functionId" type="hidden"/><br/><br/>
					
					<c:if test="${not empty extendFilterRules}">
					<div class="filterRule">
						<h3>Extend Filter Rules</h3>
						<c:forEach items="${extendFilterRules}" var="extendFilterRule">
							
								<h4>Extend from ${extendFilterRule.bizGroupName}&nbsp;&nbsp;
								<c:if test="${extendFilterRule.editAble==1}">
								<a href="<c:url value='/web/editFilterRule'/>?bizGroupId=${extendFilterRule.bizGroupId}&functionId=${command.functionId}">Edit Filter Rule</a>
								</c:if></h4>
								<c:forEach items="${extendFilterRule.filterRules}" var="extendRule">
									<div class="ruleBolck">
										Field Name: <b>${extendRule.fieldName }</b> &nbsp;
										Comparator: <b>${extendRule.comparator }</b> &nbsp;
										Value: <b>${extendRule.compareData }</b> &nbsp;&nbsp;
									</div>
								</c:forEach>
							
						</c:forEach>
					 </div>
					</c:if>
					<div class="filterRule">
					<h3>My Filter Rules</h3>
					<button type="button"
						class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only"
						onclick="addNewRule()">
						<span class="ui-button-text">Add New Rule</span>
					</button>
					
					<script type="text/javascript">
						function addNewRule() {
							$('#disableDialog').dialog('open');
						}
						function editRule(id, fieldName, comparator, value) {
							$('#operation2').val('editRule');
							$('#editRuleId').val(id);
							$('#updateRuleFieldName').val(fieldName);
							$('#updateRuleComparator').val(comparator);
							$('#updateRuleValue').val(value);
							$('#editFilterRuleDialog').dialog('open');
						}
						function deleteRule(id) {
							$('#operation1').val('deleteRule');
							$('#operationRuleId').val(id);
							if (confirm("Are you sure to delete the rule?")) {
								$('#editFilterRuleform').submit();
							}
						}
					</script>
					<c:if test="${not empty command.filterRules}">
						<c:forEach items="${command.filterRules}" var="oneFilterRule">
							<div class="ruleBolck">
								Field Name: <b>${oneFilterRule.fieldName }</b> &nbsp;
								Comparator: <b>${oneFilterRule.comparator }</b> &nbsp;
								Value: <b>${oneFilterRule.compareData }</b> &nbsp;&nbsp;
								<a href="#" onclick="editRule('${oneFilterRule.ruleId }','${oneFilterRule.fieldName }','${oneFilterRule.comparator }','${oneFilterRule.compareData }')">Edit</a>&nbsp;&nbsp;<a href="#" onclick="deleteRule('${oneFilterRule.ruleId }')">Delete</a>
							</div>
						</c:forEach>

					</c:if>
					</div>
				</c:if>
			</c:if>
		</form:form>
		<div style="height:1px;width:740px;"></div>
	</div>
</div>

<%@include file="/WEB-INF/views/includes/adminfooter.jsp"%>